package com.itheima.health.controller;

import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "套餐管理开发")
public class SetmealController {


    // springmvc  消息转化器   @ResponseBody  默认 全字段序列化

    @Reference
    private SetmealService setmealService;


    @PostMapping("setmeal/findPage")
    @ApiOperation(value = "分页查询功能",notes = "分页查询所有的套餐列表信息")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        return new Result(setmealService.findPage(queryPageBean));
    }

    @PostMapping("setmeal/upload")
    @ApiOperation(value = "分页查询功能",notes = "查询所有的检查组列表信息")
                       //与前端上传规则name值 ==》"imgFile" 相同
    public Result upload(@RequestParam("imgFile") MultipartFile file) throws IOException {
        //  获取上传文件真实名称
        String originalFilename = file.getOriginalFilename();
        // 基于真实文件扩展名 生成uuid名称
        String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);
        //  文件上传oss
        //  图片存储 集合 + 单独保存一个过期时间
        AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidfilename);
        // 表单未提交前 所有图片存储到set集合中
        RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);
        //设置所有图片有效时间
        RedisUtil.set(RedisConstant.SINGLE_PIC+uuidfilename,uuidfilename,1,TimeUnit.HOURS);
        return new Result(uuidfilename);
    }


    @PostMapping("setmeal/add")
    @ApiOperation(value = "添加功能",notes = "添加套餐")
    public Result  add(@RequestBody SetmealDTO setmealDTO ){
        return  new Result(setmealService.saveUpdate(setmealDTO));

    }
}





